package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 
 * @author zhufeng
 * @date 2023/4/14 9:41
 * @version V1.0
 */
public class 驼峰式匹配_1023 {

    @Test
    public void testCamelMatch () {
        String[] queries = {"kqshppmjgjfB","mkqsthpypmgB","kqshppmtgttB","lklqsphppmgB","kqesshppwmgB","kqshpzlcpmgB","kqsyhppmhfgB"};
        String pattern = "kqshppmgB";
        List<Boolean> booleans = camelMatch(queries, pattern);
        System.out.println(booleans);
    }

    public List<Boolean> camelMatch(String[] queries, String pattern) {
        List<Boolean> result = new ArrayList<>();
        for (String query : queries) {
            result.add(check(query, pattern));
        }
        return result;
    }

    private boolean check(String query, String pattern) {
        int p = 0;
        int q = 0;
        if (query.length() < pattern.length()) {
            return false;
        }
        boolean flag = false;
        for (int i = 0; i < query.length(); i++) {
            // 1. q 没完，p 完了
            if (p >= pattern.length()) {
                q = i;
                flag = true;
                break;
            }
            if (pattern.charAt(p) == query.charAt(i)) {
                p++;
            } else {
                if (Character.isUpperCase(query.charAt(i))) {
                    return false;
                }
            }
            if (i == query.length() -1) {
                // 2. q 完了，p 完了
                if (p >= pattern.length()) {
                    flag = true;
                }
                q = i+1;
                break;
            }
        }
        for (int i = q; i < query.length(); i++) {
            if (Character.isUpperCase(query.charAt(i))) {
                return false;
            }
        }
        return flag;
    }
}
